<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addpostRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editor(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="page.pagesize"
                :total="total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyInfo.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyInfo.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
    <!-- 添加，编辑 -->
    <el-dialog :visible="showDialog" title="新增角色" @close="btnCancel">
      <el-form ref="addDept" label-width="120px" :model="roleFrom" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="roleFrom.name"
            style="width: 80%"
            placeholder="1-6个字符"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            v-model="roleFrom.description"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <!-- 列被分为24 -->
        <el-col :span="6">
          <el-button type="primary" size="small" @click="postRole">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible="showPerm" @close="btnPermCancel">
      <!--
          default-expand-all 默认展开所有
          show-checkbox 显示多选框
          check-strictly 断开父子节点的选项关联
        -->
      <el-tree
        ref="permTree"
        node-key="id"
        :data="permList"
        :props="{label: 'name'}"
        default-expand-all
        show-checkbox
        check-strictly
      />
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-row>
      </template>
    </el-dialog></div>
</template>

<script>
import { getCompany, getRoleList, delRole, postRole, getrole, putrole, assignPerm } from '@/api/setting'
import { listToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  data() {
    return {
      page: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      roleList: [], // 角色信息
      companyInfo: {}, // 公司信息
      showDialog: false, // 弹窗显示控制
      roleFrom: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          {
            max: 6,
            message: '角色名称不能超过6个字符',
            trigger: 'blur'
          }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' },
          {
            max: 50,
            message: '角色描述不能超过50个字符',
            trigger: 'blur'
          }
        ]
      },
      showPerm: false,
      roleId: '',
      permList: []
    }
  },
  created() {
    this.getCompany()
    this.getRoleList()
    this.getPermissionList()
    // console.log(this.companyInfo, 72)
  },
  methods: {
    // 公司信息
    async getCompany() {
      this.companyInfo = await getCompany(this.$store.state.user.userInfo.companyId)
    },
    // 角色信息
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.roleList = rows
      this.total = total
    },
    // 分页组件
    currentChange(newPage) {
      // console.log(newPage)
      this.page.page = newPage
      this.getRoleList(this.page)
    },
    // 删除角色
    async delRole(id) {
      try {
        await this.$confirm('是否确认删除')
        await delRole(id)
        this.$message.success('删除成功')
        if (this.roleList.length === 1 && this.page.page > 1) {
          this.page.page--
        }
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭弹窗
    btnCancel() {
      this.roleFrom = {
        name: '',
        description: ''
      }
      // 1. 清理数据和校验错误
      this.$refs.addDept.resetFields()
      // 2. 关闭弹窗
      this.showDialog = false
    },
    // 点击新增
    addpostRole() {
      this.showDialog = true
    },
    // 点击编辑
    async editor(id) {
      this.roleFrom = await getrole(id)
      console.log(this.roleFrom)
      this.showDialog = true
    },
    // 新增角色
    async postRole() {
      // 校验
      await this.$refs.addDept.validate()
      if (this.roleFrom.id) {
        await putrole(this.roleFrom)
      } else {
        await postRole(this.roleFrom)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 页面需要更新
      this.getRoleList()
      this.btnCancel()
    },
    // 获取权限
    async getPermissionList() {
      this.permList = listToTreeData(await getPermissionList(), '0')
    },
    // 开启权限弹窗
    assignPerm(roleId) {
      this.roleId = roleId
      this.showPerm = true
    },
    // 关闭权限弹窗
    btnPermCancel() {
      // 3.关闭弹窗
      this.showPerm = false
    },
    // 给角色添加权限
    async btnPermOK() {
      await assignPerm({ id: this.roleId, permIds: this.$refs.permTree.getCheckedKeys() })
      // 提示用户
      this.$message.success('提交成功')
      // 关闭弹窗
      this.showPerm = false
    }
  }
}
</script>

<style>

</style>
